import React, { useState, useEffect } from 'react';
import { DotLoading, Result,Tag } from 'antd-mobile'
import { ACCESSTOKEN, checkAuthorize } from '@/utils/authorize';
import { useRequest, request } from 'alita';

export  const OtherCountInPartake =   ({ title, url,color,zeroColor, sourceId }: { title: string, url: string,  color: string,zeroColor?:string, sourceId: number }) => {
    const [ing, setIng] = useState<boolean>(true);
    const [fail, setFail] = useState<string|undefined>(undefined)
    const [count, setCount] = useState<number>(0);
    function load() {
        setIng(true);
        setFail(undefined);
        request(`${url}/other-count/${sourceId}`, { headers: { 'Authorization': `Bearer ${localStorage.getItem(ACCESSTOKEN)}` }, }).then(({ data, code, msg }: { data: any, code: number, msg: string }) => {
            if (code === 0) {
                setCount(data);
            } else {
                setFail(msg);
            }
            setIng(false);
        })
    }
    useEffect(load,[url,sourceId ])
    if (ing) {
        return <DotLoading />;
    }
    if (fail) {
        <Result
            status='error'
            title='无法完成操作'
            description={fail}
        />
    }
    return <Tag round color={count >0? color:zeroColor||color}>{title}({count})</Tag>
}


export  const SourceCountInPartake =   ({ title, url,color,zeroColor, otherId }: { title: string, url: string,  color: string,zeroColor?:string, otherId: number }) => {
    const [ing, setIng] = useState<boolean>(true);
    const [fail, setFail] = useState<string|undefined>(undefined)
    const [count, setCount] = useState<number>(0);
    function load() {
        setIng(true);
        setFail(undefined);
        request(`${url}/source-count/${otherId}`, { headers: { 'Authorization': `Bearer ${localStorage.getItem(ACCESSTOKEN)}` }, }).then(({ data, code, msg }: { data: any, code: number, msg: string }) => {
            if (code === 0) {
                setCount(data);
            } else {
                setFail(msg);
            }
            setIng(false);
        })
    }
    useEffect(load,[url,otherId ])
    if (ing) {
        return <DotLoading />;
    }
    if (fail) {
        <Result
            status='error'
            title='无法完成操作'
            description={fail}
        />
    }
    return <Tag style={{padding:'3px'}} round color={count >0? color:zeroColor||color}>{title}({count})</Tag>
}

